package cn.com.dycc.party_work.transfer.service;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.common.attach.dao.AttachDao;
import cn.com.dycc.common.attach.entity.Attach;
import cn.com.dycc.party_work.education.entity.DeptAndEducation;
import cn.com.dycc.party_work.education.entity.Education;
import cn.com.dycc.party_work.education.service.EduNotFoundException;
import cn.com.dycc.party_work.transfer.dao.TransferDao;
import cn.com.dycc.party_work.transfer.entity.Transfer;
import cn.com.dycc.party_work.transfer.entity.TransferInfo;
import cn.com.dycc.party_work.transfer.entity.TransferParam;
import cn.com.dycc.party_work.transfer.param.FindTransferParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.FileUtil;
import cn.com.dycc.util.Paging;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
 * Description: 
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年7月10日 上午9:50:07.
 * Update Date Time: 
 * @see
 */
@Service("transService")
public class TransferServiceImpl implements TransferService{
	@Resource
	private TransferDao dao;	
	
	@Resource
	private AttachDao attachDao;
	
	@Resource(name="paging")
	private Paging paging;
	
	public int addTransfer(TransferParam param, HttpServletRequest req) throws TransferNotFoundException {
		HttpSession session = req.getSession();
		String user_id = (String) session.getAttribute("id");
		if(user_id==null){
			throw new TransferNotFoundException("请重新登录！");
		}
		if(param==null){
			throw new TransferNotFoundException("请选择迁入或迁出");
		}
		if(param.getTransfer_name()==null||param.getTransfer_name().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁移标题");
		}
		if(param.getUser_id()==null||param.getUser_id().trim().isEmpty()){
			throw new TransferNotFoundException("请填写党员");
		}
		if(param.getUser_name()==null||param.getUser_name().trim().isEmpty()){
			throw new TransferNotFoundException("请填写党员姓名");
		}
		/*if(param.getDept_id_out()==null||param.getDept_id_out().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁出党支部");
		}*/
		if(param.getDept_name_out()==null||param.getDept_name_out().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁出党支部名称");
		}
		/*if(param.getDept_id_in()==null||param.getDept_id_in().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入党支部");
		}*/
		if(param.getDept_name_in()==null||param.getDept_name_in().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入党支部名称");
		}
		if(param.getDate_out()==null||param.getDate_out().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁出时间");
		}
		if(param.getDate_in()==null||param.getDate_in().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入时间");
		}
		boolean b = DateUtil.compareToDate(
				DateUtil.format_StringToDate(param.getDate_in()),
				DateUtil.format_StringToDate(param.getDate_out()));
		if(!b){
			throw new TransferNotFoundException("迁出时间不能大于迁入时间");
		}
		if(param.getTransfer_flag()==null||param.getTransfer_flag().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入迁出标识");
		}
		if(param.getTransfer_des()==null||param.getTransfer_des().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁移说明");
		}
		param.setCreate_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		param.setOpt_user_id(user_id);
		param.setOrg_id((String)session.getAttribute("org_id"));
		
		int n = dao.addTransfer(param);
		if(n!=1){
			throw new TransferNotFoundException("添加失败");
		}
		
		List<Attach> listAttach = new ArrayList<Attach>();
		
		if(param.getListAttach()!=null&&!param.getListAttach().trim().isEmpty()){
			String str=param.getListAttach().trim();
			JSONArray json=JSONArray.fromObject(str);
        
			for(int i=0;i<json.size();i++){

				Object check=json.get(i);
				if("null".equals(check.toString())){
					continue;
				}
				JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
				
	
				String attach_ext=(String)job.get("attach_ext");
				String attach_path=(String)job.get("attach_path");
				String attach_size=(String)job.get("attach_size");
				String title=(String)job.get("title");
				Attach attach=new Attach();
				attach.setMessage_no(param.getTransfer_id());
				attach.setAttach_type(10);
				attach.setTitle(title);
				attach.setAttach_ext(attach_ext);
				attach.setAttach_path(attach_path);
				attach.setAttach_size(attach_size);				
				attach.setOrg_id((String)session.getAttribute("org_id"));
				attach.setOpt_user_id(user_id);
				listAttach.add(attach);
								
			}        
        }
		List<Attach> newListAttach=FileUtil.moveFile(listAttach,"party_work");
			
		if(newListAttach!=null){
		for(Attach attach:newListAttach){
			int a = attachDao.insertAttach(attach);
			}
		}
		return n;
	}

	public int deleteTransfer(String[] transfer_ids, HttpServletRequest req) throws TransferNotFoundException {
		if(transfer_ids==null){
			throw new TransferNotFoundException("选择要删除的关系");
		}
		if(transfer_ids.length<=0){
			throw new TransferNotFoundException("没有要删除的关系");
		}
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new TransferNotFoundException("重新登录");
		}
		List<String> list = Arrays.asList(transfer_ids);
		int n = dao.deleteTransfer(org_id,list);
		/*if(n<=0){
			throw new TransferNotFoundException("删除失败");
		}*/
		return n;
	}

	public int updateTransfer(TransferParam param, HttpServletRequest req) throws TransferNotFoundException {
		HttpSession session = req.getSession();
		String user_id = (String) session.getAttribute("id");
		String org_id = (String)session.getAttribute("org_id");
		if(user_id==null){
			throw new TransferNotFoundException("请重新登录！");
		}
		if(param==null){
			throw new TransferNotFoundException("请选择迁入或迁出");
		}
		if(param.getTransfer_id()==null||param.getTransfer_id().trim().isEmpty()){
			throw new TransferNotFoundException("找不到ID");
		}
		if(param.getTransfer_name()==null||param.getTransfer_name().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁移标题");
		}
		if(param.getUser_id()==null||param.getUser_id().trim().isEmpty()){
			throw new TransferNotFoundException("请填写党员");
		}
		if(param.getUser_name()==null||param.getUser_name().trim().isEmpty()){
			throw new TransferNotFoundException("请填写党员姓名");
		}
		/*if(param.getDept_id_out()==null||param.getDept_id_out().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁出党支部");
		}*/
		if(param.getDept_name_out()==null||param.getDept_name_out().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁出党支部名称");
		}
		/*if(param.getDept_id_in()==null||param.getDept_id_in().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入党支部");
		}*/
		if(param.getDept_name_in()==null||param.getDept_name_in().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入党支部名称");
		}
		if(param.getDate_out()==null||param.getDate_out().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁出时间");
		}
		if(param.getDate_in()==null||param.getDate_in().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入时间");
		}
		if(param.getTransfer_flag()==null||param.getTransfer_flag().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁入迁出标识");
		}
		if(param.getTransfer_des()==null||param.getTransfer_des().trim().isEmpty()){
			throw new TransferNotFoundException("请填写迁移说明");
		}
		
		Transfer oldTransfer = dao.findTransfer(param.getTransfer_id());
		if(oldTransfer==null){
			throw new TransferNotFoundException("ID错误");
		}
		
		TransferParam newParam = new TransferParam();
		if(!oldTransfer.getTransfer_name().equals(param.getTransfer_name())){
			newParam.setTransfer_name(param.getTransfer_name());
		}
		if(!oldTransfer.getUser_id().equals(param.getUser_id())){
			newParam.setUser_id(param.getUser_id());
		}
		if(!oldTransfer.getUser_name().equals(param.getUser_name())){
			newParam.setUser_name(param.getUser_name());
		}
		if(!oldTransfer.getDept_id_out().equals(param.getDept_id_out())){
			newParam.setDept_id_out(param.getDept_id_out());
		}
		if(!oldTransfer.getDept_name_out().equals(param.getDept_name_out())){
			newParam.setDept_name_out(param.getDept_name_out());
		}
		
		if(!oldTransfer.getDept_id_in().equals(param.getDept_id_in())){
			newParam.setDept_id_in(param.getDept_id_in());
		}
		if(!oldTransfer.getDept_name_in().equals(param.getDept_name_in())){
			newParam.setDept_name_in(param.getDept_name_in());
		}
		
		if(!oldTransfer.getDate_out().equals(param.getDate_out())){
			newParam.setDate_out(param.getDate_out());
		}
		if(!oldTransfer.getDate_in().equals(param.getDate_in())){
			newParam.setDate_in(param.getDate_in());
		}
		
		/*if(!oldTransfer.getState().equals(param.getState())){
			newParam.setState(param.getState());
		}*/
		newParam.setOpt_user_id(user_id);
		newParam.setOrg_id(org_id);		
		newParam.setTransfer_id(param.getTransfer_id());
		int n = dao.updateTransfer(newParam);
		if(n!=1){
			throw new EduNotFoundException("修改失败");
		}
		
		List<Attach> listAttach = new ArrayList<Attach>();
		
		if(param.getListAttach()!=null&&!param.getListAttach().trim().isEmpty()){
			String str=param.getListAttach().trim();
			JSONArray json=JSONArray.fromObject(str);
        
			for(int i=0;i<json.size();i++){

				Object check=json.get(i);
				if("null".equals(check.toString())){
					continue;
				}
				JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
				
	
				String attach_ext=(String)job.get("attach_ext");
				String attach_path=(String)job.get("attach_path");
				String attach_size=(String)job.get("attach_size");
				String title=(String)job.get("title");
				Attach attach=new Attach();
				attach.setMessage_no(param.getTransfer_id());
				attach.setAttach_type(10);
				attach.setTitle(title);
				attach.setAttach_ext(attach_ext);
				attach.setAttach_path(attach_path);
				attach.setAttach_size(attach_size);				
				attach.setOrg_id((String) session.getAttribute("org_id"));
				attach.setOpt_user_id(user_id);
				listAttach.add(attach);
								
			}        
        }
		List<Attach> newListAttach=FileUtil.moveFile(listAttach,"party_work");
			
		if(newListAttach!=null){
		for(Attach attach:newListAttach){
			int a = attachDao.insertAttach(attach);
			}
		}
		return n;
	}

	public List<TransferInfo> findTransferByParam(FindTransferParam param, HttpServletRequest req)
			throws TransferNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new TransferNotFoundException("重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);
		/*String dept_id = (String) session.getAttribute("dept_id");
		param.setDept_id(dept_id);*/
		param.setOrg_id(org_id);
		List<TransferInfo> list = dao.findTransferByParam(param);
		
		return list;		
	}

	public String findTransferCountByParam(FindTransferParam param, HttpServletRequest req)
			throws TransferNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new TransferNotFoundException("重新登录");
		}
		param.setOrg_id(org_id);
		String str = dao.findTransferCountByParam(param);
		return str;
	}
	
}
